/**
  ******************************************************************************
  * @file    tc32l010_pca.h
	* @author  MCU Software Team
	* @Version V1.2.0
  * @Date    2021-01-11
  * @brief   Header file of PCA HAL module.
  ******************************************************************************
 **/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __XS32L010_PCA_H
#define __XS32L010_PCA_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "tc32l010.h"
#include "tc32l010_rcc.h"

/** @addtogroup PCA
  * @{
  */

/* Exported types ------------------------------------------------------------*/
/** @defgroup PCA_Exported_Types PCA Exported Types
  * @{
  */
/**
  * @brief  PCA Configuration Structure definition
	* @note   This structure is for PCA 	
  */
typedef struct
{
  uint16_t ClkSrcSel;                   /*!< Specifies input capture clock source.
                                             This parameter can be a value of @ref PCA_ClkSrcSel */		
	
  uint16_t RunInIdleMode;               /*!< PCA is enabled or disabled in IDLE mode.
                                             This parameter can be a value of @ref PCA_RunInIdleMode */	

  uint16_t Period;                      /*!< Specifies the period value to be loaded into the active
                                             Auto-Reload Register at the immediate.
                                             This parameter must be a number between 0x0000 and 0xFFFF.  */ 
} PCA_BaseInitTypeDef;



/**
  * @brief  PCA Input Capture Configuration Structure definition
  */
typedef struct
{
  uint16_t  ICPolarity;                 /*!< Specifies the active edge of the input signal.
                                             This parameter can be a value of @ref PCA_Input_Capture_Polarity */

} PCA_ICInitTypeDef;


/**
  * @brief  PCA Output Compare Configuration Structure definition
  */
typedef struct
{
  uint16_t  CompareEnable;   /*!< Specifies the output compare function enable or disable.
                                  This parameter can be a value of @ref PCA_Output_Compare_Enable */

  uint16_t 	TogEnable;     	 /*!< Specifies tog function enable or disable.
                                  TogEnable & PwmEnable must not be enabled at the same time.
                                  This parameter can be a value of @ref PCA_TogEnable */		

  uint16_t 	MatEnable;     	 /*!< Specifies tog function enable or disable.
                                  TogEnable & PwmEnable must not be enabled at the same time.	
                                  This parameter can be a value of @ref PCA_MatEnable */	
	
  uint16_t 	PwmEnable;     	 /*!< Specifies tog function enable or disable.
                                  TogEnable & PwmEnable must not be enabled at the same time.	
                                  This parameter can be a value of @ref PCA_PwmEnable */			

  uint16_t 	b16PwmEnable;    /*!< Specifies tog function enable or disable.
                                  This parameter can be a value of @ref PCA_b16PwmEnable */	
                                  
  uint16_t 	Period;          /*!< Specifies the period value compared with CNT register.
                                  Period caculation = Period/(PCA clock)
                                  This parameter can be a number between Min_Data = 0x00 and Max_Data = 0xFFFF. */	
	
} PCA_OCInitTypeDef;


/**
  * @}
  */ /* End of group PCA_Exported_Types */


/* Exported constants --------------------------------------------------------*/
/** @defgroup PCA_Exported_Constants PCA Exported Constants
  * @{
  */

#define IS_PCA_ALL_PERIPH(PERIPH) (((PERIPH) == PCA)) 

/** @defgroup PCA_ClkSrcSel PCA input capture clock source select
  * @{
  */
#define PCA_CLOCK_SOURCE_PCLKDIV32                  (PCA_MOD_CPS_0)	            								    /*!< Specify PCLK divider 32 as pca input clock source */
#define PCA_CLOCK_SOURCE_PCLKDIV16                  (PCA_MOD_CPS_1)                    								/*!< Specify PCLK divider 16 as pca input clock source */
#define PCA_CLOCK_SOURCE_PCLKDIV8                   (PCA_MOD_CPS_2)													/*!< Specify PCLK divider 8 as pca input clock source */
#define PCA_CLOCK_SOURCE_PCLKDIV4                   (PCA_MOD_CPS_3)                                                 /*!< Specify PCLK divider 4 as pca input clock source */
#define PCA_CLOCK_SOURCE_PCLKDIV2                   (PCA_MOD_CPS_4)	                                                /*!< Specify PCLK divider 2 as pca input clock source */
#define PCA_CLOCK_SOURCE_TIM0_OVERFLOW              (PCA_MOD_CPS_5)	                                                /*!< Specify TIM0 overflow as pca input clock source */
#define PCA_CLOCK_SOURCE_TIM1_OVERFLOW              (PCA_MOD_CPS_6)	                                                /*!< Specify TIM1 overflow as pca input clock source */
#define PCA_CLOCK_SOURCE_ECI                        (PCA_MOD_CPS_7)                                                 /*!< Specify ECI as pca input clock source */

#define IS_PCA_CLK_SEL(SEL)     ((((SEL) & 0xF1) == 0x00) && ((SEL) ! = 0x00))
/**
  * @}
  */

/** @defgroup PCA_RunInIdleMode PCA is enabled or disabled in IDLE mode.
  * @{
  */
#define PCA_IDLEMODE_DISABLE						(0x10)	
#define PCA_IDLEMODE_ENABLE							(0x00)	

#define IS_PCA_CIDL_MODE(MODE)   (((MODE) == PCA_IDLEMODE_DISABLE)||((MODE) == PCA_IDLEMODE_ENABLE))
/**
  * @}
  */


/** @defgroup PCA_Input_Capture_Polarity PCA input capture polarity selection
	* @note	CCAPMx(1,2,3,4)'s position is same as CCAPM0
  * @{
  */
#define PCA_INPUT_POLARITY_NONE						(0x00000000)	
#define PCA_INPUT_POLARITY_FALLING					(PCA_CCAPMx_CAPN)
#define PCA_INPUT_POLARITY_RISING					(PCA_CCAPMx_CAPP)
#define PCA_INPUT_POLARITY_BOTH						(PCA_CCAPMx_CAPN | PCA_CCAPMx_CAPP)

/**
  * @}
  */


/** @defgroup PCA_Interrupt_Flags PCA interrupt flags
  * @{
  */
#define PCA_FLAG_CC0                                (PCA_CR_CCF0)              			
#define PCA_FLAG_CC1                                (PCA_CR_CCF1)        	              
#define PCA_FLAG_CC2                                (PCA_CR_CCF2)  
#define PCA_FLAG_CC3                                (PCA_CR_CCF3)  
#define PCA_FLAG_CC4                                (PCA_CR_CCF4)  
#define	PCA_FLAG_OVERFLOW                           (PCA_CR_CF)

#define IS_PCA_GET_FLAG(PCA_FLAG)                   (((PCA_FLAG) == PCA_FLAG_CC0) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC1) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC2) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC3) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC4) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_OVERFLOW))

#define IS_PCA_CLEAR_FLAG(PCA_FLAG)                 (((PCA_FLAG) == PCA_FLAG_CC0) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC1) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC2) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC3) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_CC4) ||\
                                                     ((PCA_FLAG) == PCA_FLAG_OVERFLOW))

/**
  * @}
  */


/** @defgroup PCA_Interrupt_Enable PCA interrupt enable
  * @{
  */
#define PCA_IT_CC0                                  (0x01)
#define PCA_IT_CC1                                  (0x02)
#define PCA_IT_CC2                                  (0x04)
#define PCA_IT_CC3                                  (0x08)
#define PCA_IT_CC4                                  (0x10)
#define PCA_IT_OVERFLOW                             (0x80)

#define IS_PCA_GET_IT(PCA_IT)                       (((PCA_IT) == PCA_IT_CC0) ||\
                                                     ((PCA_IT) == PCA_IT_CC1) ||\
                                                     ((PCA_IT) == PCA_IT_CC2) ||\
                                                     ((PCA_IT) == PCA_IT_CC3) ||\
                                                     ((PCA_IT) == PCA_IT_CC4) ||\
                                                     ((PCA_IT) == PCA_IT_OVERFLOW))

#define IS_PCA_IT(PCA_IT)                           (((PCA_IT) == PCA_IT_CC0) ||\
                                                     ((PCA_IT) == PCA_IT_CC1) ||\
                                                     ((PCA_IT) == PCA_IT_CC2) ||\
                                                     ((PCA_IT) == PCA_IT_CC3) ||\
                                                     ((PCA_IT) == PCA_IT_CC4) ||\
                                                     ((PCA_IT) == PCA_IT_OVERFLOW))

/**
  * @}
  */


/** @defgroup PCA_Channel_Num PCA input capture channel number
  * @{
  */
#define PCA_CHANNEL_0 							   (0x01)
#define PCA_CHANNEL_1 							   (0x02)
#define PCA_CHANNEL_2 							   (0x04)
#define PCA_CHANNEL_3                              (0x08)
#define PCA_CHANNEL_4 							   (0x10)

/**
  * @}
  */


/** @defgroup PCA_Output_Compare_Enable PCA output compare function enable/disable
  * @{
  */
#define PCA_OC_ENABLE                   (PCA_CCAPMx_ECOM)      			        /*!< output compare function enable */
#define PCA_OC_DISABLE                  0x00000000U                      		/*!< output compare function disable */

/**
  * @}
  */

/** @defgroup PCA_MatEnable PCA tog function enable/disable
  * @{
  */
#define PCA_MAT_ENABLE              	(PCA_CCAPMx_MAT)                        /*!< Mat function enable */
#define PCA_MAT_DISABLE           		0x00000000U                      		/*!< Mat function disable */
/**
  * @}
  */

/** @defgroup PCA_TogEnable PCA tog function enable/disable
  * @{
  */
#define PCA_TOG_ENABLE              	(PCA_CCAPMx_MAT | PCA_CCAPMx_TOG)       /*!< Tog function enable */
#define PCA_TOG_DISABLE           		0x00000000U                      		/*!< Tog function disable */
/**
  * @}
  */


/** @defgroup PCA_PwmEnable PCA pwm function enable/disable
  * @{
  */
#define PCA_PWM_ENABLE              	(PCA_CCAPMx_PWM)              			/*!< Pwm function enable */
#define PCA_PWM_DISABLE           		0x00000000U                      		/*!< Pwm function disable */
/**
  * @}
  */

/** @defgroup PCA_b16PwmEnable PCA pwm function enable/disable
  * @{
  */
#define PCA_EPWM_ENABLE              	(PCA_CCAPMx_EPWM)              			/*!< Pwm function enable */
#define PCA_EPWM_DISABLE           		0x00000000U                      		/*!< Pwm function disable */
/**
  * @}
  */

/**
  * @}
  */ /* End of group PCA_Exported_Constants */



/* Exported functions --------------------------------------------------------*/
/** @addtogroup PCA_Exported_Functions
  * @{
  */

/** @addtogroup PCA_Exported_Functions_Group
 * @{
 */
/* PCA functions ********************************************************/
void PCA_BaseInit(PCA_TypeDef* PCAx, PCA_BaseInitTypeDef* PCA_BaseInitStruct);
void PCA_BaseDeInit(PCA_TypeDef* PCAx);
void PCA_Start(PCA_TypeDef* PCAx);
void PCA_Start_IT(PCA_TypeDef* PCAx, uint32_t intid);	
void PCA_Stop(PCA_TypeDef* PCAx);
void PCA_Stop_IT(PCA_TypeDef* PCAx, uint32_t intid);

/* Blocking mode: Polling */
void PCA_OC_Start(PCA_TypeDef* PCAx, uint32_t Channel);
void PCA_OC_Stop(PCA_TypeDef* PCAx, uint32_t Channel);

/* interrupt */
void PCA_OC_Start_IT(PCA_TypeDef* PCAx, uint32_t Channel);
void PCA_OC_Stop_IT(PCA_TypeDef* PCAx, uint32_t Channel);

void PCA_IC_ConfigChannel(PCA_TypeDef* PCAx, PCA_ICInitTypeDef* sConfig, uint32_t Channel);
void PCA_OC_ConfigChannel(PCA_TypeDef* PCAx, PCA_OCInitTypeDef* sConfig, uint32_t Channel);
void PCA_OC_PolarityInversionCmd(PCA_TypeDef* PCAx, uint32_t Channel, FunctionalState NewState);
void PCA_SetAutoreload(PCA_TypeDef* PCAx, uint16_t Autoreload);
void PCA_SetCounter(PCA_TypeDef* PCAx, uint16_t cnt);
uint16_t PCA_GetCounter(PCA_TypeDef* PCAx);
FlagStatus PCA_GetOut(PCA_TypeDef* PCAx, uint32_t Channel);
void PCA_SetCcap(PCA_TypeDef* PCAx, uint32_t Channel, uint16_t Value);
uint16_t PCA_GetCcap(PCA_TypeDef* PCAx, uint32_t Channel);
void PCA_SetCcapHL(PCA_TypeDef* PCAx, uint32_t Channel, uint8_t ValueH, uint8_t ValueL);
void PCA_GetCcapHL(PCA_TypeDef* PCAx, uint32_t Channel, uint8_t* ValueH, uint8_t* ValueL);

FlagStatus PCA_GetFlagStatus(PCA_TypeDef* PCAx, uint16_t PCA_FLAG);
void PCA_ClearFlag(PCA_TypeDef* PCAx, uint16_t PCA_FLAG);
ITStatus PCA_GetITStatus(PCA_TypeDef* PCAx, uint16_t PCA_IT);
void PCA_ClearITPendingBit(PCA_TypeDef* PCAx, uint16_t PCA_IT);

/**
  * @}
  */ /* End of group PCA_Exported_Functions_Group */

/**
  * @}
  */ /* End of group PCA_Exported_Functions */


/* Exported macros -----------------------------------------------------------*/



/* Private Macros -----------------------------------------------------------*/


/* Private Functions --------------------------------------------------------*/
/** @addtogroup PCA_Private_Functions
 * @{
 */


/**
  * @}
  */ /* End of group PCA_Private_Functions */




/**
  * @}
  */ /* End of group PCA */


#ifdef __cplusplus
}
#endif

#endif /* __XS32L030_PCA_H */
